#include "commerrno.h"
#include "CRoleAttrCfg.h"
#include "../mainctrl.h"

int CRoleAttrCfg::GetIndexByID(int iID)
{
    int iRet = 0;
    int iCareer = 0;
    int iBaseType = 0;

    iRet = CMainCtrl::m_pstMain->m_stRoleMgr.GetCareerBaseTypeByID(
        iCareer, iBaseType, iID);
    if (iRet != 0)
    {
        return -1;//因为返回下标 所以这里必须返回负数
    }

    return (iCareer - 1) * CAREER_END + iBaseType - 1;
}

int CRoleAttrCfg::FindConfig(int iID)
{
    int iIndex = 0;
    iIndex = GetIndexByID(iID);
    if (iIndex < 0)
    {
        CT_WARNING(("Career Attr id(%d) invalid!",iID));
        return -1;//因为返回下标 所以这里必须返回负数
    }

    if (iIndex >= _astRoleAttrCfg.Size())
    {
        CT_WARNING(("Career Attr id(%d) index(%d) invalid array size(%d)",
                    iID,iIndex,_astRoleAttrCfg.Size()));
        return -1;//因为返回下标 所以这里必须返回负数
    }

    if (_astRoleAttrCfg[iIndex].GetID() != iID)
    {
        CT_WARNING(("Career Attr id(%d) index(%d) not equal id(%d)",
                    iID,iIndex,_astRoleAttrCfg[iIndex].GetID()));
        return -1;//因为返回下标 所以这里必须返回负数
    }

    return iIndex;
}

int CRoleAttrCfg::AddConfig(CRoleAttrCfgData& stCfgData)
{
    int iRet = 0;
    int iIndex = 0;

    if (!stCfgData.IsNonNegativeData())
    {
        CT_WARNING(("Career Attr id(%d) config data error",stCfgData.GetID()));
        return ERROR_ROLE_CAREER_ATTR_ERROR_CFG;
    }

    iIndex = GetIndexByID(stCfgData.GetID());
    CT_CHECK(iIndex, ERROR_ROLE_CAREER_ATTR_ID_INVALID);

    if (iIndex >= _astRoleAttrCfg.Size())
    {
        CT_WARNING(("Career Attr id(%d) index(%d) invalid array size(%d)",
                    stCfgData.GetID(),iIndex,_astRoleAttrCfg.Size()));
        return ERROR_ROLE_CAREER_ATTR_ID_INVALID;
    }
    
    iRet = CMainCtrl::m_pstMain->m_stRoleMgr.CheckValidCareerAttr(
        _astRoleAttrCfg[iIndex].GetID());
    if (iRet == 0)//说明ID重复
    {
        return ERROR_ROLE_CAREER_ATTR_ID_REPEAT;
    }

    _astRoleAttrCfg[iIndex] = stCfgData;

    return 0;
}

CRoleAttrCfgData& CRoleAttrCfg::GetConfig(int iIndex)
{
    return _astRoleAttrCfg[iIndex];
}

